package flexconsole.model {
	import flash.events.EventDispatcher;
	
	/**
	 * Dispatched when the model property changes.
	 *
	 * @eventType mx.events.PropertyChangeEvent.PROPERTY_CHANGE
	 */
	[Event(name="propertyChange", type="mx.events.PropertyChangeEvent")]
	
	public class LogLevelModel extends EventDispatcher {
		
		static public const DEBUG:String = "DEBUG";
		
		static public const INFO:String = "INFO";
		
		static public const WARN:String = "WARN";
		
		static public const ERROR:String = "ERROR";
		
		static public const FATAL:String = "FATAL";
		
		[Bindable]
		public var debug:Boolean = true;
		
		[Bindable]
		public var info:Boolean = true;
		
		[Bindable]
		public var warn:Boolean = true;
		
		[Bindable]
		public var error:Boolean = true;
		
		[Bindable]
		public var fatal:Boolean = true;
		
		
		public function LogLevelModel(value:Object = null) {
			super();
			
			if (value) {
				read(value);
			}
		}
		
		public function isEnabled(logLevel:String):Boolean {
			var n:String = logLevel.toString().toLowerCase();
			return hasOwnProperty(n) ? this[n] : false;
		}
		
		public function write():Object {
			return { debug: debug, info: info, warn: warn, error: error, fatal: fatal };
		}
		
		public function read(value:Object):void {
			var properties:Array = [ "debug", "info", "warn", "error", "fatal" ];
			var n:String;
			
			for each (n in properties) {
				if (value.hasOwnProperty(n)) {
					this[n] = value[n];
				} else {
					throw new Error("value does not have property: " + n);
				}
			}
		}
	
	}
}